package com.dfd.store.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.dfd.store.dao.UserDao;
import com.dfd.store.domain.User;
import com.dfd.store.utils.JDBCUtils;

public class UserDaoImpl implements UserDao {
	private QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());

	/**
	 * 用户注册
	 * 
	 * @throws Exception
	 */
	@Override
	public void userRigist(User user) throws Exception {
		String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
		Object[] params = { user.getUid(), user.getUsername(), user.getPassword(), user.getName(), user.getEmail(),
				user.getTelephone(), user.getBirthday(), user.getSex(), user.getState(), user.getCode() };
		runner.update(sql, params);
	}

	/**
	 * 用户激活
	 * 
	 * @throws SQLException
	 */
	@Override
	public User userActive(String code) throws SQLException {

		String sql = "select * from user where code = ?";
		User user = runner.query(sql, new BeanHandler<User>(User.class), code);
		return user;
	}

	/**
	 * 用户更新
	 * 
	 * @throws SQLException
	 */
	@Override
	public void updateUser(User user) throws SQLException {
		String sql = "update user set username = ? , password = ?,name = ? ,email = ? , telephone = ? ,birthday = ? ,sex = ?, state = ?,code = ? where uid = ?";
		Object[] params = { user.getUsername(), user.getPassword(), user.getName(), user.getEmail(),
				user.getTelephone(), user.getBirthday(), user.getSex(), user.getState(), user.getCode(),
				user.getUid() };
		runner.update(sql, params);
	}
	
	/**
	 * 用户登录
	 */
	@Override
	public User userLogin(User user) throws SQLException {
		String sql = "select * from user where username = ? and password = ?";
		User uu = runner.query(sql, new BeanHandler<User>(User.class), user.getUsername(), user.getPassword());
		return uu;
	}

}
